﻿package com.moonshineproject.plugin {
	import com.moonshineproject.GlobalEventDispatcher;
	import com.moonshineproject.IDEModel;
	import com.moonshineproject.plugin.console.view.ConsoleModeEvent;
	
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	
	import com.moonshineproject.plugin.console.ConsoleOutputter;
	
	public class PluginBase extends ConsoleOutputter implements IPlugin 
	{
		protected namespace console;
		
		override public function get name():String			{ throw new Error("You need to give a unique name.") }
		public function get author():String			{ return "N/A"; }
		public function get description():String	{ return "A plugin base that plugins can extend to gain easier access to some functionality."; }
		
		/**
		 * ensures if the plugin will be activated by default when the plugin 
		 * is loaded for the first time (without settings xml file written)
		 * */
		public function get activatedByDefault():Boolean { return true; }
		
		console static var commands:Dictionary = new Dictionary(true);
		console static var mode:String = "";

		protected var dispatcher:EventDispatcher = GlobalEventDispatcher.getInstance();
		protected var model:IDEModel = IDEModel.getInstance();
		
		protected var _activated:Boolean = false;
		public function get activated():Boolean 
		{
			return _activated;
		}
		
		public function activate():void
		{
			_activated = true;
		}
		public function deactivate():void
		{
			_activated = false;
		}
		
		
		public function PluginBase() {}
		
		// Console command functions
		protected function registerCommand(commandName:String, callback:Function):void
		{
			console::commands[commandName] = callback;
		}
		
		protected function unregisterCommand(commandName:String):void
		{
			delete console::commands[commandName];
		}
		
		protected function enterConsoleMode(newMode:String):void
		{
			console::mode = newMode;
			dispatcher.dispatchEvent(new ConsoleModeEvent(ConsoleModeEvent.CHANGE, newMode));
		}
		
		protected function exitConsoleMode():void
		{
			console::mode = "";
			dispatcher.dispatchEvent(new ConsoleModeEvent(ConsoleModeEvent.CHANGE, ""));
		}
		
		
	}
}